package com.mgface.zookeeper.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.EnsurePath;

/**
 * 确保数据节点存在的机制<br/>
 * 采用静默的方式创建，如果节点存在就不进行任何操作，也不对外抛出异常，否则正常创建数据节点
 * 
 * @author user
 *
 */
public class EnsurePathUse {
	private static final String path = "/ensure/zk";
	private static CuratorFramework client = CuratorFrameworkFactory.builder().sessionTimeoutMs(10_000)
			.connectString("localhost:2181,localhost:2182,localhost:2183").connectionTimeoutMs(10_000)
			.retryPolicy(new ExponentialBackoffRetry(1_000, 3)).build();

	public static void main(String[] args) throws Exception {
		client.start();
		client.usingNamespace("ensure");
		EnsurePath ensurePath = new EnsurePath(path);
		ensurePath.ensure(client.getZookeeperClient());
		ensurePath.ensure(client.getZookeeperClient());
		EnsurePath ensurePath2 = client.newNamespaceAwareEnsurePath("/zk");
		ensurePath2.ensure(client.getZookeeperClient());
		System.out.println(client.getChildren().forPath("/ensure"));
	}

}
